package cn.sc.allinpay.allfavour.business.setsettle;

import android.app.Activity;
import android.content.DialogInterface;

import java.math.BigDecimal;
import java.util.List;

import cn.pai.common.listener.OnPromptListener;
import cn.pai.common.utils.AmtUtils;
import cn.pai.mvp.callback.MpCallBack;
import cn.pai.mvp.intervenor.Intervenor;
import cn.pai.mvp.presenter.PaiPresenter;
import cn.sc.allinpay.allfavour.business.common.data.SettleData;
import cn.sc.allinpay.allfavour.business.common.data.SettleResult;
import cn.sc.allinpay.allfavour.module.device.DeviceLogin;
import cn.sc.allinpay.allfavour.module.printer.IPrinter;
import cn.sc.allinpay.allfavour.module.printer.PrintData;
import cn.sc.allinpay.allfavour.module.printer.PrintManager;

/**
 * description：结算
 * author：pany
 * on 2018/10/24 10:04
 */
public class SetSettlePresenter extends PaiPresenter<SetSettleContract.View> implements SetSettleContract.Presenter {

    private SetSettleModel model = new SetSettleModel();
    private List<SettleData> settleDatas;
    private IPrinter printer;
    private PrintData printData;

    @Override
    protected Intervenor createIntervenor() {
        return new Intervenor() {

            @Override
            public void onResume() {
                super.onResume();
                DeviceLogin.login(view.getContext());
            }

            @Override
            public void onPause() {
                super.onPause();
                DeviceLogin.logout();
            }
        };
    }

    @Override
    public void start() {
                        view.showBasicInfo(model.getGlobal().getConfigData().getMerName(), model.getGlobal().getConfigData().getTerId(),
                        model.getGlobal().getCashierData().getCashierNo(), model.getGlobal().getCashierData().getCashierName());
        view.loading("数据统计中...");


        model.querySettlement(new MpCallBack<SettleResult, String>() {
            @Override
            public void onSuccess(SettleResult resp) {
                settleDatas = resp.getSettleDataList();
                view.loaded();
//                view.transAdapterChange(resp);



                view.showSettleTime(resp.getTvSettleTime(), resp.getTvSettleTime2());
                view.showSale(resp.getTvSaleNum(), resp.getTvSale());
                view.showVoid(resp.getTvVoidNum(), resp.getTvVoid());
                view.showPaid(resp.getTvPaid());
                view.showMemberDis(resp.getTvMemberDis());
                view.showCoupon(resp.getTvCoupon());
                view.showWallet(resp.getTvWallet());
                view.showCash(resp.getTvCash());
                view.showOnline(resp.getTvOnline());

                int totalNum = 0;
                BigDecimal totalAmt = BigDecimal.ZERO;

                for (SettleData settleData : settleDatas) {
                    if (settleData.getOilName().equals("0#")) {
                        totalNum = totalNum + settleData.getSaleNum() - settleData.getVoidNum();
                        totalAmt = totalAmt.add(settleData.getSaleAmt().subtract(settleData.getVoidAmt()));
                        view.showtv0(settleData.getSaleNum() - settleData.getVoidNum() + "笔", AmtUtils.formatY(settleData.getSaleAmt().subtract(settleData.getVoidAmt())));
                    }

                    if (settleData.getOilName().equals("92#")) {
                        totalNum = totalNum + settleData.getSaleNum() - settleData.getVoidNum();
                        totalAmt = totalAmt.add(settleData.getSaleAmt().subtract(settleData.getVoidAmt()));
                        view.showtv1(settleData.getSaleNum() - settleData.getVoidNum() + "笔", AmtUtils.formatY(settleData.getSaleAmt().subtract(settleData.getVoidAmt())));
                    }

                    if (settleData.getOilName().equals("95#")) {
                        totalNum = totalNum + settleData.getSaleNum() - settleData.getVoidNum();
                        totalAmt = totalAmt.add(settleData.getSaleAmt().subtract(settleData.getVoidAmt()));
                        view.showtv2(settleData.getSaleNum() - settleData.getVoidNum() + "笔", AmtUtils.formatY(settleData.getSaleAmt().subtract(settleData.getVoidAmt())));
                    }

                    if (settleData.getOilName().equals("98#")) {
                        totalNum = totalNum + settleData.getSaleNum() - settleData.getVoidNum();
                        totalAmt = totalAmt.add(settleData.getSaleAmt().subtract(settleData.getVoidAmt()));
                        view.showtv3(settleData.getSaleNum() - settleData.getVoidNum() + "笔", AmtUtils.formatY(settleData.getSaleAmt().subtract(settleData.getVoidAmt())));
                    }

                    if (settleData.getOilName().equals("LNG")) {
                        totalNum = totalNum + settleData.getSaleNum() - settleData.getVoidNum();
                        totalAmt = totalAmt.add(settleData.getSaleAmt().subtract(settleData.getVoidAmt()));
                        view.showtv4(settleData.getSaleNum() - settleData.getVoidNum() + "笔", AmtUtils.formatY(settleData.getSaleAmt().subtract(settleData.getVoidAmt())));
                    }

                    if (settleData.getOilName().equals("CNG")) {
                        totalNum = totalNum + settleData.getSaleNum() - settleData.getVoidNum();
                        totalAmt = totalAmt.add(settleData.getSaleAmt().subtract(settleData.getVoidAmt()));
                        view.showtv5(settleData.getSaleNum() - settleData.getVoidNum() + "笔", AmtUtils.formatY(settleData.getSaleAmt().subtract(settleData.getVoidAmt())));
                    }

                    if (settleData.getOilName().equals("非油品")) {
                        view.showtv6(settleData.getSaleNum() - settleData.getVoidNum() + "笔", AmtUtils.formatY(settleData.getSaleAmt().subtract(settleData.getVoidAmt())));
                    }

                    if (settleData.getOilName().equals("其它")) {
                        view.showtv7(settleData.getSaleNum() - settleData.getVoidNum() + "笔", AmtUtils.formatY(settleData.getSaleAmt().subtract(settleData.getVoidAmt())));
                    }
                }

                view.showtvTotal(totalNum + "笔", AmtUtils.formatY(totalAmt));

                printData = new PrintData();
                printData.setSettleTime2(resp.getTvSettleTime2());
                printData.setSettleTime(resp.getTvSettleTime());
                printData.setSaleNum(resp.getTvSaleNum());
                printData.setSale(resp.getTvSale());
                printData.setVoidNum(resp.getTvVoidNum());
                printData.setVoidAmt(resp.getTvVoid());
                printData.setPaidAmt(resp.getTvPaid());
                printData.setMemberDisAmt(resp.getTvMemberDis());
                printData.setCouponAmt(resp.getTvCoupon());
                printData.setWalletAmt(resp.getTvWallet());
                printData.setCash(resp.getTvCash());
                printData.setOnlineAmt(resp.getTvOnline());
                printData.setTotalNum(totalNum + "笔");
                printData.setTotalAmt( AmtUtils.formatY(totalAmt));
            }

            @Override
            public void onFailure(String resp, String message) {
                view.loaded();
                view.toast(message);
            }
        });
    }


    @Override
    public void settlement() {
        view.loading("结算中...");
        model.delTransRecord(new MpCallBack<Integer, String>() {
            @Override
            public void onSuccess(Integer resp) {
                view.toast("结算完成");
                if (settleDatas != null) {
                    print();
                } else {
                    view.finish();
                }
            }

            @Override
            public void onFailure(String resp, String message) {
                view.prompt(message);
            }
        });
    }

    public void print() {
        //因为onActivityResult回来没有立即执行onResume方法
        DeviceLogin.login(view.getContext());
        view.loading("结算完成，正在打印...");

        printData.setMerchantName(model.getGlobal().getConfigData().getMerName());
        printData.setTerminalId(model.getGlobal().getConfigData().getTerId());
        printData.setOperNo(model.getGlobal().getCashierData().getCashierNo());
        printData.setOperName(model.getGlobal().getCashierData().getCashierName());
        printData.setSettleData(settleDatas);
        printer = PrintManager.createPrinter(PrintManager.PRINT_SETTLE, printData);
        printer.print(new IPrinter.OnPrintListener() {
            @Override
            public void onStart(int step) {

            }

            @Override
            public void onSuccess(int step) {
                view.loaded();
                view.prompt(false, "打印成功", new OnPromptListener("确定") {
                    @Override
                    public void onPositive(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        view.setResult(Activity.RESULT_OK, null);
                        view.finish();
                    }
                });
            }

            @Override
            public void onFail(int code, String msg) {
                view.loaded();
                view.prompt(false, msg + ",是否继续打印", new OnPromptListener("确定", "取消") {
                    @Override
                    public void onPositive(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        print();
                    }

                    @Override
                    public void onNegative(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        view.setResult(Activity.RESULT_OK, null);
                        view.finish();
                    }
                });
            }
        });
    }
}
